package com.pur.zsms.shopmanage.web;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.pur.zsms.accountmanage.vo.AccountVo;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.company.service.ICompanyService;
import com.pur.zsms.matermanage.mater.service.IMaterService;
import com.pur.zsms.matermanage.mater.vo.MaterValueVo;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.matermanage.mater.vo.ShoppingCartMaterVo;
import com.pur.zsms.pmsnmanage.user.po.UserLevelPo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.service.IUserService;
import com.pur.zsms.po.SupplierShopPo;
import com.pur.zsms.shopmanage.service.IShopService;
import com.pur.zsms.shopmanage.vo.ShopRaisePriceVo;
import com.pur.zsms.shopmanage.vo.ShopVo;
import com.pur.zsms.supplier.service.ISupplierService;
import com.pur.zsms.systemsetting.datadictionary.service.IDataDictionaryService;
import com.pur.zsms.vo.SupplierShopVo;

@Controller
@RequestMapping(value = "/shop")
public class ShopControllor {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(ShopControllor.class);

	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Resource(name = "supplierService")
	private ISupplierService supplierService;

	@Resource(name = "shopService")
	private IShopService shopService;

	@Resource(name = "userService")
	private IUserService userService;

	@RequestMapping(value = "/shopList.do", method = { RequestMethod.POST })
	@ResponseBody
	public String shopList(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, ShopVo shopVo,
			HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		if (shopVo.getCompanyId() == 0) {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			UserLevelPo tUserLevelPo = userService.getUserLevelPo(userInfo.getUserId());
			shopVo.setCompanyId((int) tUserLevelPo.getCompanyUser());
		}
		this.pageList = shopService.listShop(pageController, shopVo);
		List<ShopVo> shopList = new ArrayList<ShopVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				ShopVo f = new ShopVo();
				f.setShopId((Integer) item[0]);
				f.setShopName((String) item[1]);
				f.setCompanyUserName((String) item[2]);
				f.setShopUserList((String) item[3]);
				f.setCode(item[4].toString());
				shopList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", shopList);
		return json.toString();
	}

	@RequestMapping(value = "/preSaveShop.do", method = { RequestMethod.GET })
	public ModelAndView preSaveShop() {
		/*
		 * JSONObject json = new JSONObject(); List<RoleVo> roleList = null;
		 * 
		 * try { roleList = foodService.listAllRole(); //查询所有角色信息 } catch
		 * (GenericException e) { e.printStackTrace(); }
		 */
		Map map = new HashMap();
		// map.put("rolelist", roleList);
		return new ModelAndView("/shop/addShop", map);
	}

	@RequestMapping(value = "/preSaveShopForSup.do", method = { RequestMethod.GET })
	public ModelAndView preSaveShopForSup(int companyId) {
		Map map = new HashMap();
		map.put("companyId", companyId);
		return new ModelAndView("/shop/addShopForSup", map);
	}

	@RequestMapping(value = "/saveShop.do", method = { RequestMethod.POST })
	@ResponseBody
	public String saveShop(ShopVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			/*
			 * UserPo userInfo = (UserPo) session.getAttribute("user"); int
			 * supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			 * c.setSupId(supId);
			 */
			UserPo lgUser = (UserPo) session.getAttribute("user");
			boolean flag = shopService.addShop(c, lgUser);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("success", false);
				json.put("msg", shopService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/saveShopForSup.do", method = { RequestMethod.POST })
	@ResponseBody
	public String saveShopForSup(ShopVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {

			UserPo userInfo = (UserPo) session.getAttribute("user");
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());

			c.setSupId(supId);
			c.setSupName(supplierService.getSupPoBySupId(supId).getCompanyName());

			UserPo lgUser = (UserPo) session.getAttribute("user");
			boolean flag = shopService.addShopForSup(c, lgUser);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("success", false);
				json.put("msg", shopService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/preUpdateShop.do", method = { RequestMethod.GET })
	public ModelAndView preUpdateShop(long shopId) {
		JSONObject json = new JSONObject();
		ShopVo shopVo = new ShopVo();
		try {
			shopVo = shopService.getShopVoByShopId(shopId);
		} catch (GenericException e) {
			e.printStackTrace();
		}
		Map map = new HashMap();
		map.put("shopVo", shopVo);
		return new ModelAndView("/shop/updateShop", map);
	}

	@RequestMapping(value = "/preUpdateShopForShop.do", method = { RequestMethod.GET })
	public ModelAndView preUpdateShopForShop(long shopId, HttpSession session) {
		JSONObject json = new JSONObject();
		ShopVo shopVo = new ShopVo();
		try {
			shopVo = shopService.getShopVoByShopId(shopId);

			UserPo userInfo = (UserPo) session.getAttribute("user");
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			shopVo.setSupId(supId);

			SupplierShopVo supplierShopVo = new SupplierShopVo();
			supplierShopVo.setSupId(supId);
			supplierShopVo.setShopId(shopId);

			SupplierShopPo supplierShopPo = supplierService.getSupplierShopPo(supplierShopVo).get(0);
			shopVo.setCode(supplierShopPo.getCode());
			shopVo.setSupName(supplierShopPo.getSupName());

		} catch (GenericException e) {
			e.printStackTrace();
		}
		Map map = new HashMap();
		map.put("shopVo", shopVo);
		return new ModelAndView("/shop/updateShopForSup", map);
	}

	@RequestMapping(value = "/updateShop.do", method = { RequestMethod.POST })
	@ResponseBody
	public String updateShop(ShopVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			boolean flag = shopService.updateShop(c);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("success", false);
				json.put("msg", shopService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/updateShopForSup.do", method = { RequestMethod.POST })
	@ResponseBody
	public String updateShopForSup(ShopVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			c.setSupId(supId);
			boolean flag = shopService.updateShopForSup(c);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("success", false);
				json.put("msg", shopService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/getShopIdByUserId.do", method = { RequestMethod.POST })
	@ResponseBody
	public String getShopIdByUserId(HttpSession session) {
		JSONObject json = new JSONObject();
		UserPo lgUser = (UserPo) session.getAttribute("user");
		try {
			long shopId = shopService.getShopIdoByUserId(lgUser.getUserId());
			json.put("shopId", shopId);
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/getShopIdBycompanyId.do", method = { RequestMethod.POST })
	@ResponseBody
	public String getShopIdBycompanyId(HttpSession session, String companyId) {
		JSONObject json = new JSONObject();
		json.put("success", true);
		try {
			String userTestValue = shopService.getShopIdBycompanyId(companyId);
			json.put("stringJson", userTestValue);

		} catch (GenericException e) {
			e.printStackTrace();
		}
		return json.toString();

	}

	@RequestMapping(value = "/getShopIdBycompanyIdSupId.do", method = { RequestMethod.POST })
	@ResponseBody
	public String getShopIdBycompanyIdSupId(HttpSession session, String companyId) {
		JSONObject json = new JSONObject();
		json.put("success", true);
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			String userTestValue = shopService.getShopIdBycompanyIdSupid(companyId, supId);
			json.put("stringJson", userTestValue);

		} catch (GenericException e) {
			e.printStackTrace();
		}
		return json.toString();

	}
	@RequestMapping(value = "/getShopIdBycompanyIdAllSupId.do", method = { RequestMethod.POST })
	@ResponseBody
	public String getShopIdBycompanyIdAllSupId(HttpSession session, String companyIdAll) {
		JSONObject json = new JSONObject();
		json.put("success", true);
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			String userTestValue = shopService.getShopIdBycompanyIdAllSupId(companyIdAll, supId);
			json.put("stringJson", userTestValue);

		} catch (GenericException e) {
			e.printStackTrace();
		}
		return json.toString();

	}

	@RequestMapping(value = "/preShopRaisePrice.do", method = { RequestMethod.GET })
	public ModelAndView preShopRaisePrice(long shopId) {
		JSONObject json = new JSONObject();
		ShopVo shopVo = new ShopVo();
		try {
			shopVo = shopService.getShopVoByShopId(shopId);

		} catch (GenericException e) {
			e.printStackTrace();
		}
		Map map = new HashMap();
		map.put("shopVo", shopVo);
		return new ModelAndView("/shop/shopRaiseIndex", map);
	}

	@RequestMapping(value = "/shopRaiseList.do", method = { RequestMethod.POST })
	@ResponseBody
	public String shopRaiseList(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			ShopRaisePriceVo shopRaisePriceVo) throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = shopService.shopRaiseList(pageController, shopRaisePriceVo);
		List<ShopRaisePriceVo> shopRaisePriceVoList = new ArrayList<ShopRaisePriceVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				ShopRaisePriceVo f = new ShopRaisePriceVo();
				f.setRpid(Long.valueOf(item[0].toString()));
				f.setShopId(Long.valueOf(item[1].toString()));
				f.setShopName(item[2].toString());
				f.setSupId(Integer.valueOf(item[3].toString()));
				f.setSupName(item[4].toString());
				f.setMaterType((Integer) item[5]);
				f.setMaterTypeName((String) item[6]);
				f.setRaiseAmount(new BigDecimal(item[7].toString()));

				shopRaisePriceVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", shopRaisePriceVoList);
		return json.toString();
	}

	@RequestMapping(value = "/preSaveShopRaise.do", method = { RequestMethod.GET })
	public ModelAndView preSaveShopRaise(long shopId) {
		JSONObject json = new JSONObject();
		ShopVo shopVo = new ShopVo();
		try {
			shopVo = shopService.getShopVoByShopId(shopId);

		} catch (GenericException e) {
			e.printStackTrace();
		}
		Map map = new HashMap();
		map.put("shopVo", shopVo);
		return new ModelAndView("/shop/addShopRaise", map);
	}

	@RequestMapping(value = "/saveShopRaise.do", method = { RequestMethod.POST })
	@ResponseBody
	public String saveShopRaise(ShopRaisePriceVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			/*
			 * UserPo userInfo = (UserPo) session.getAttribute("user"); int
			 * supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			 * c.setSupId(supId);
			 */
			UserPo lgUser = (UserPo) session.getAttribute("user");
			boolean flag = shopService.addShopRaise(c, lgUser);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("success", false);
				json.put("msg", shopService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/preUpdateShopRaise.do", method = { RequestMethod.GET })
	public ModelAndView preUpdateShopRaise(long shopId, long rpid) {
		JSONObject json = new JSONObject();
		ShopVo shopVo = new ShopVo();
		ShopRaisePriceVo shopRaisePriceVo = new ShopRaisePriceVo();

		try {
			shopVo = shopService.getShopVoByShopId(shopId);
			shopRaisePriceVo = shopService.getShopRaisePriceVoByRpid(rpid);
		} catch (GenericException e) {
			e.printStackTrace();
		}
		Map map = new HashMap();
		map.put("shopVo", shopVo);
		map.put("shopRaisePriceVo", shopRaisePriceVo);
		return new ModelAndView("/shop/updateShopRaise", map);
	}

	@RequestMapping(value = "/updateShopRaise.do", method = { RequestMethod.POST })
	@ResponseBody
	public String updateShopRaise(ShopRaisePriceVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo lgUser = (UserPo) session.getAttribute("user");
			boolean flag = shopService.updateShopRaise(c, lgUser);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("success", false);
				json.put("msg", shopService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/deleteShopRaise.do", method = { RequestMethod.POST })
	@ResponseBody
	public String deleteShopRaise(long rpid) {
		JSONObject json = new JSONObject();
		try {
			shopService.deleteShopRaise(rpid);
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	@RequestMapping(value = "/shopListForSup.do", method = { RequestMethod.POST })
	@ResponseBody
	public String shopListForSup(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, ShopVo shopVo,
			HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		if (shopVo.getCompanyId() == 0) {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			UserLevelPo tUserLevelPo = userService.getUserLevelPo(userInfo.getUserId());
			shopVo.setCompanyId((int) tUserLevelPo.getCompanyUser());
		}
		UserPo userInfo = (UserPo) session.getAttribute("user");
		int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
		shopVo.setSupId(supId);
		this.pageList = shopService.listShopForSup(pageController, shopVo);
		List<ShopVo> shopList = new ArrayList<ShopVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				ShopVo f = new ShopVo();
				f.setShopId((Integer) item[0]);
				f.setShopName((String) item[1]);
				f.setCompanyUserName((String) item[2]);
				f.setShopUserList((String) item[3]);
				if (item[4] != null) {
					f.setCode(item[4].toString());
				} else {
					f.setCode("0");
				}
				shopList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", shopList);
		return json.toString();
	}
	
	@RequestMapping(value = "/checkShop.do", method = { RequestMethod.POST })
	@ResponseBody
	public String checkShop(String value) {
		JSONObject json = new JSONObject();
		try {
			boolean flag = shopService.getCheckPhone(value);
			if(flag){
				json.put("success", true);
			}else{
				json.put("success", false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

}
